﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BusinessLayer.WorldArtPrice.Models;
using CommandLib;
using WorldArtPrice.Models;

namespace WorldArtPrice.Controllers
{
    public class ArtistController : Controller
    {
        public ActionResult Index(string keyWord, int? born, int? died, int? country, int? pageIndex)
        {
            ArtistSearchResultModel advancedSearchModel = new ArtistSearchResultModel();
            advancedSearchModel.KeyWord = Server.UrlDecode(keyWord == null ? string.Empty : keyWord);
            advancedSearchModel.ArtistsName1 = Server.UrlDecode(keyWord == null ? string.Empty : keyWord);
            advancedSearchModel.ArtistsName2 = Server.UrlDecode(keyWord == null ? string.Empty : keyWord);
            advancedSearchModel.Born = born;
            advancedSearchModel.Died = died;
            advancedSearchModel.CountryId = country;
            advancedSearchModel.PageIndex = Convert.ToInt32(pageIndex == null ? 1 : pageIndex);
            advancedSearchModel.ListArtists = new ArtistBL().Search(Server.UrlDecode(keyWord == null ? string.Empty : keyWord), Convert.ToInt32(born == null ? -1 : born), Convert.ToInt32(died == null ? -1 : died), Convert.ToInt32(country ==null ? -1 : country), Convert.ToInt32(pageIndex == null ? 1 : pageIndex), 30);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] == null)
            {
                var totalRecord = 0;
                new ArtistBL().GetCountByKeyWord(Server.UrlDecode(keyWord == null ? string.Empty : keyWord), Convert.ToInt32(born == null ? -1 : born), Convert.ToInt32(died == null ? -1 : died), Convert.ToInt32(country == null ? -1 : country), out totalRecord);
                Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] = totalRecord;
            }
            advancedSearchModel.TotalRecord = Convert.ToInt32(Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] != null ? Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] : 0);
            List<Countrys> listCoutryModel = new CountryBL().GetAll();
            listCoutryModel.Insert(0, new Countrys { Id = -1, Country = "All countries ----------" });
            advancedSearchModel.ListCoutry = listCoutryModel;            
            return View(advancedSearchModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(ArtistSearchResultModel advancedSearchModel)
        {
            
            if (advancedSearchModel.Born != null)
            {
                try
                {
                    int temp = Convert.ToInt32(advancedSearchModel.Born) % 2;
                    if (Convert.ToInt32(advancedSearchModel.Born) <=0)
                    {
                        ModelState.AddModelError("Born", "Born wrong format");
                    }
                }
                catch {
                    ModelState.AddModelError("Born", "Born wrong format");
                }
            }

            if (advancedSearchModel.Died != null)
            {
                try
                {
                    int temp = Convert.ToInt32(advancedSearchModel.Died) % 2;
                    if (Convert.ToInt32(advancedSearchModel.Died) <= 0)
                    {
                        ModelState.AddModelError("Died", "Died wrong format");
                    }
                }
                catch
                {
                    ModelState.AddModelError("Died", "Died wrong format");
                }
            }
            if (advancedSearchModel.Born == null && advancedSearchModel.CountryId == -1 && advancedSearchModel.Died == null && string.IsNullOrWhiteSpace(advancedSearchModel.ArtistsName1) && string.IsNullOrWhiteSpace(advancedSearchModel.ArtistsName2))
            {                
                ModelState.AddModelError("ArtistsName", "Name must be min. one letter");                
            }

            if (ModelState.IsValid == false)
            {
                advancedSearchModel.ListArtists = new ArtistBL().Search(string.Empty,-1,-1,-1 ,1 , 30);
                if ((advancedSearchModel.PageIndex == 0 ? 1 : advancedSearchModel.PageIndex) == 1 || Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] == null)
                {
                    var totalRecord = 0;
                    new ArtistBL().GetCountByKeyWord(string.Empty,-1,-1,-1, out totalRecord);
                    Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] = totalRecord;
                }
                advancedSearchModel.TotalRecord = Convert.ToInt32(Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] != null ? Session[Constant.SESSION_TOTAL_RECORD_ARTISTS_SEARCH] : 0);

                List<Countrys> listCoutry = new CountryBL().GetAll();
                listCoutry.Insert(0, new Countrys { Id = -1, Country = "All countries ----------" });
                advancedSearchModel.ListCoutry = listCoutry;
            
                return View(advancedSearchModel);
            }
            else
            {
                return RedirectToAction("Index", "Artist", new { keyword = Server.UrlEncode(string.IsNullOrEmpty(advancedSearchModel.ArtistsName1) ? advancedSearchModel.ArtistsName2 : advancedSearchModel.ArtistsName1), born = advancedSearchModel.Born , died = advancedSearchModel.Died, country = advancedSearchModel.CountryId, pageIndex = 1 });
            }
            
        }

    }
}
